﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace MG_POS_PC.Util
{
    public class FormatUtil
    {
        #region string2int
        public static int String2Int(string str)
        {
            return Convert.ToInt32(str);
        }

        public static string Int2String(int a)
        {
            return Convert.ToString(a);
        }
        #endregion

        #region get set textbox
        public static void setTxt(TextBox txt, string str, bool enable)
        {
            txt.Text = str.Trim();
            txt.Enabled = enable;            
        }

        public static string getTxt(TextBox txt)
        {
            return txt.Text.ToString().Trim();
        }
        #endregion

        #region get set NumericUpDown
        public static void setUD(NumericUpDown ud, int value, bool enable)
        {
            ud.Value = Convert.ToDecimal(value);
            ud.Enabled = enable;
        }

        public static int getUD(NumericUpDown ud)
        {
            return (int)ud.Value;
        }
        #endregion

        #region get set ComboBox
        public static void setCombobox(ComboBox cmb, string value, bool enable)
        {
            cmb.Text = value;
            cmb.Enabled = enable;
        }

        public static string getCombobox(ComboBox cmb)
        {
            return cmb.Text.Trim();
        }
        #endregion


        public static DataTable GenerateDataTable(string xmlName)
        {
            DataTable dt = new DataTable();
            string[] header = {"Station","Name","IP","Connection"};
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlName);
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("IPs").ChildNodes;

                for (int i = 0; i < header.Length; i++)
                {
                    DataColumn dc = new DataColumn(header[i]);
                    dt.Columns.Add(dc);
                }

                for (int k = 0; k < nodeList.Count; k++ )
                {
                    if (nodeList[k].Attributes.Count > 0)
                    {
                        DataRow dr = dt.NewRow();
                        
                        dr[header[0]] = nodeList[k].Attributes[header[0]].Value;
                        dr[header[1]] = nodeList[k].Attributes[header[1]].Value;
                        dr[header[2]] = nodeList[k].Attributes[header[2]].Value;
                        dr[header[3]] = "";
                        dt.Rows.Add(dr);
                    }
                }
                return dt;
            }
            catch (System.Exception ex)
            {
                writeLog(ex.Message);
            }
            return null;
        }

        public static void GenerateXML(DataTable dt, string xmlName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlName);
            XmlNode root = xmlDoc.SelectSingleNode("IPs");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                XmlElement xe1 = xmlDoc.CreateElement("IP");
                xe1.SetAttribute(dt.Columns[0].ColumnName.ToString().Trim(), Convert.ToString(dt.Rows[i][0]).Trim());
                xe1.SetAttribute(dt.Columns[1].ColumnName.ToString().Trim(), Convert.ToString(dt.Rows[i][1]).Trim());
                xe1.SetAttribute(dt.Columns[2].ColumnName.ToString().Trim(), Convert.ToString(dt.Rows[i][2]).Trim());
                xe1.SetAttribute(dt.Columns[3].ColumnName.ToString().Trim(), "");
                root.AppendChild(xe1);
            }
            xmlDoc.Save(xmlName);
        }

        private static void writeLog(string mess)
        {
            FileStream fs = new FileStream(@"c:/mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(" -->" + mess);
            m_streamWriter.Flush();
            m_streamWriter.Close();
            fs.Close();
        }
        
    }
}
